<template>
  <div class="app-container">

      <el-row :gutter="20">
        <!--章节数据-->
        <el-col :span="4" :xs="24">
          <h5>教材章节</h5>
          <div class="head-container">
            <el-tree
              :data="nodeOptions"
              :props="defaultProps"
              :expand-on-click-node="false"
              ref="tree"
              default-expand-all
              @node-click="handleNodeClick"
            />
          </div>
        </el-col>
        <!--用户数据-->
        <el-col :span="20" :xs="24">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-if="node != null"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['teach:bookContent:add']"
        >创建新版本</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['teach:bookContent:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bookContentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="章节" align="center" prop="node.title" />
      <el-table-column label="创建教师" align="center" prop="user.nickName" />
      <el-table-column label="最后更新时间" align="center" prop="addTime" width="180">
        <template slot-scope="scope">
          <span v-if="scope.row.updateTime == null">{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          <span v-if="scope.row.updateTime != null">{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否当前版本" align="center" prop="isCurrent" :formatter="isCurrentFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['teach:bookContent:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['teach:bookContent:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

        </el-col>
    </el-row>
    <!-- 添加或修改教材内容对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="说明" prop="comment">
          <el-input v-model="form.comment" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="当前版本">
          <el-radio-group v-model="form.isCurrent">
            <el-radio
              v-for="dict in isCurrentOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBookContent, getBookContent, delBookContent, addBookContent, updateBookContent } from "@/api/teach/bookContent";
import { listNodeTree, getNode } from "@/api/teach/node";
import { getBook } from "@/api/teach/book";
import Editor from '@/components/Editor';

export default {
  name: "BookContent",
  components: {
    Editor,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教材内容表格数据
      bookContentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否当前版本字典
      isCurrentOptions: [],
      // 章节列表
      nodeOptions: [],
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bookId: null,
        nodeId: null,
        isCurrent: null,
      },
      // 表单参数
      form: {},
      book: null,
      node: null,
      // 表单校验
      rules: {
        content: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    const id = this.$route.params && this.$route.params.bookId;
    getBook(id).then(response => {
      this.book = response.data;
    });

    this.queryParams.bookId = id;
    this.getList();
    this.getDicts("sys_yes_no").then(response => {
      this.isCurrentOptions = response.data;
    });
    listNodeTree(id).then(response => {
      this.nodeOptions = response.data;
    });
  },
  methods: {
    /** 查询教材内容列表 */
    getList() {
      this.loading = true;
      listBookContent(this.queryParams).then(response => {
        this.bookContentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 是否当前版本字典翻译
    isCurrentFormat(row, column) {
      return this.selectDictLabel(this.isCurrentOptions, row.isCurrent);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bookId: this.book.id,
        nodeId: this.node == null ? null : this.node.id,
        content: null,
        isCurrent: "N",
        comment: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 节点单击事件
    handleNodeClick(data) {
      getNode(data.id).then(response => {
        this.node = response.data;
      });
      this.queryParams.nodeId = data.id;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加教材内容";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBookContent(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改教材内容";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBookContent(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBookContent(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除教材内容编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delBookContent(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有教材内容数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportBookContent(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
